package top.mcwebsite.playandroid.main.ui

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import top.mcwebsite.lib.router.compose.RouteContainer
import top.mcwebsite.playandroid.main.vm.PlayAndroidAppViewModel

@Composable
fun PlayAndroidApp() {
    val navController = rememberNavController()

    val playAndroidMainScreenState: MutableState<PlayAndroidMainScreen> = remember {
        mutableStateOf(PlayAndroidMainScreen.ArticlesScreen)
    }
    val viewModel: PlayAndroidAppViewModel = viewModel()
    val uiState by viewModel.uiStateFlow.collectAsState()
    val systemUiController = rememberSystemUiController()
    systemUiController.setStatusBarColor(MaterialTheme.colorScheme.background, darkIcons = !uiState.isDarkMode)
    Scaffold(
        bottomBar = {
            BottomNavigationBar(playAndroidMainScreenState, navController) {
                playAndroidMainScreenState.value = it
                navController.navigate(it.route) {
                    // Pop up to the start destination of the graph to
                    // avoid building up a large stack of destinations
                    // on the back stack as users select items
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    // Avoid multiple copies of the same destination when
                    // reselecting the same item
                    launchSingleTop = true
                    // Restore state when reselecting a previously selected item
                    restoreState = true
                }
            }
        }
    ) { innerPadding ->
        RouteContainer(
            startRoute = "home",
            navController = navController,
            modifier = Modifier.padding(innerPadding))
    }
}